package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanAllVo;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ityu
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private RedisTemplate   redisTemplate;

    private final int NURSINGPLAN_STATUS_ENABLE = 1;

    private final String NURSING_PLAN_CACHE = "nursing_plan_cache";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        NursingPlan nursingPlan = this.getById(id);

        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> list2 = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo bean = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            bean.setProjectId(nursingProjectPlan.getProjectId().toString());
            bean.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return bean;
        }).collect(Collectors.toList());
        NursingPlanVo result = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        result.setProjectPlans(list2);
        return result;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        deleteCache();
        if (dto.getPlanName() == null) {
            nursingPlanMapper.updateStatusById(dto.getId(), dto.getStatus());
            return 1;
        }


            NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
            nursingPlan.setId(Integer.parseInt(dto.getId().toString()));
            this.updateById(nursingPlan);


            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            if (!CollectionUtils.isEmpty(dto.getProjectPlans())) {
                dto.getProjectPlans().forEach(nursingProjectPlan -> {
                    nursingProjectPlan.setPlanId(Long.parseLong(nursingPlan.getId().toString()));
                });
                return nursingProjectPlanService.saveBatch(dto.getProjectPlans()) ? 1 : 0;
            }

        return 1;
    }

    /**
     * 删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        deleteCache();
        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        this.removeById( id);
        if (!CollectionUtils.isEmpty(list)) {
            return (nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id))) ? 1 : 0;
        }
        return 1;
    }


    @Override
    @Transactional
    public int saveNursingPlan(NursingPlanDto dto) {
        deleteCache();
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.save(nursingPlan);

        List<NursingProjectPlan> nursingProjectPlanList = dto.getProjectPlans();

        Boolean b = true;
        if (!CollectionUtils.isEmpty(nursingProjectPlanList)){
            nursingProjectPlanList.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(Long.parseLong(nursingPlan.getId().toString()));
            });
            b = nursingProjectPlanService.saveBatch(nursingProjectPlanList);
        }
        return b ? 1:0;
    }

    @Override
    public List<NursingPlanAllVo> getAll() {
        List<NursingPlanAllVo> result = ( List<NursingPlanAllVo>) redisTemplate.opsForValue().get(NURSING_PLAN_CACHE);
        if (CollectionUtils.isNotEmpty( result)){
            return result;
        }
        List<NursingPlan> list = this.list(Wrappers.<NursingPlan>lambdaQuery().eq(NursingPlan::getStatus, NURSINGPLAN_STATUS_ENABLE));
        result = list.stream().map(nursingPlan -> {
            NursingPlanAllVo nursingPlanAllVo = BeanUtil.toBean(nursingPlan, NursingPlanAllVo.class);
            nursingPlanAllVo.setId(nursingPlan.getId().toString());
            nursingPlanAllVo.setSortNo(nursingPlan.getSortNo().toString());
            return nursingPlanAllVo;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(NURSING_PLAN_CACHE,result);
        return result;
    }

    private void deleteCache(){
        redisTemplate.delete(NURSING_PLAN_CACHE);
    }
}
